﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ExtendedReflection.Utilities.Safe.Diagnostics;

namespace PexMeHelper
{
    /// <summary>
    /// Cleans or fixes the database files of PexMe
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: PexMeHelper <Mode>");
                Console.WriteLine(@"\t Clean: cleanes the database, \n
                                    \t Prepare: Generates commands for running all PUTs, \n
                                    \t Consolidate: Generates a consolidated test file for all tests (used while generating coverage reports) \n
                                    \t AnalyzeClass: Analyzes a given class and prints all its fields and methods \n
                                    \t AnalyzeAssembly: Analyzes a given assembly and prints all its class definitions \n
                                    \t CopyNChgClass: Changes classname with the given extension and copies the file \n
                                    \t Stats: Collects statistics from the assembly file
                                    ");
                return;
            }

            if (args[0] == "Clean")
            {
                Console.WriteLine("Starting cleaning PexMe database");
                new PexMeDynamicDatabaseCleaner().CleanAllDynamicDB();
                Console.WriteLine("Finished cleaning PexMe database");
            }
            else if (args[0] == "Prepare")
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("Usage: PexMeHelper Prepare AssemblyName");
                    return;
                }

                PUTCommandGenerator.GeneratePUTCommands(args[1]);
                Console.WriteLine("Prepare DONE!!!");
            }
            else if (args[0] == "Consolidate")
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("Usage: PexMeHelper Consolidate DirectoryName");
                    return;
                }

                PexTestsFormatter.TestFormatter.Format(new string[] { args[1] });
                Console.WriteLine("Consolidate DONE!!!");
            }
            else if (args[0] == "AnalyzeClass")
            {
                if (args.Length != 3)
                {
                    Console.WriteLine("Usage: PexMeHelper Consolidate AssemblyName ClassName");
                    return;
                }

                ClassAnalyzer.AnalyzeClass(args[1], args[2]);
                Console.WriteLine("AnalyzeClass DONE!!!");
            }
            else if (args[0] == "AnalyzeAssembly")
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("Usage: PexMeHelper Consolidate AssemblyName");
                    return;
                }

                ClassAnalyzer.AnalyzeAssembly(args[1]);
                Console.WriteLine("AnalyzeAssembly DONE!!!");
            }
            else if (args[0] == "CopyNChgClass")
            {
                if (args.Length != 4)
                {
                    Console.WriteLine("Usage: PexMeHelper CopyNChgClass SrcFilename DestFilename ClassExtension");
                    return;
                }

                PexTestsFormatter.TestFormatter.ChangeAndRenameMethodNames(args[1], args[2], args[3]);
                Console.WriteLine("CopyNChgClass DONE!!!");
            }
            else if (args[0] == "Stats")
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("Usage: PexMeHelper Stats AssemblyFile");
                    return;
                }

                StatCollector.CollectStats(args[1]);
                Console.WriteLine("Stats Collection DONE!!!");
            }
            else if (args[0] == "RandoopStats")
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("Usage: PexMeHelper RandoopStats AssemblyFile");
                    return;
                }

                StatCollector.RandoopStats = true;
                StatCollector.CollectStats(args[1]);
                Console.WriteLine("Stats Collection DONE!!!");
            }
            else
            {
                Console.WriteLine("Unknown option of PexMeHelper");
            }
        }
    }
}
